package com.zcsy.manage.web.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.constants.ManageConstants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.manage.model.base.OwnerInfo;
import com.zcsy.manage.model.system.Dict;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.base.IOwnerInfoService;
import com.zcsy.manage.service.system.IDictService;
import com.zcsy.manage.web.common.LogUtils;

@Controller
@RequestMapping(value = "/ownerinfo")
public class OwnerInfoController {
	
	@Autowired
	private IOwnerInfoService ownerInfoService;

	@Autowired
	private IDictService dictService;
	
	/**正常 */
	private static final int STATE_SUCCESS      =  1;  
	/**禁用*/
	private static final int STAET_FORBIDEN     =  2;  
	
	@RequestMapping(value = "/ownerInfoList", method = RequestMethod.GET)
	public String ownerInfoList(Model model, Integer pNo, Integer pSize, String name, String lName, String phoneNo, HttpServletRequest req) {
		
		if (pNo == null) {
			pNo = Constants.PAGE_NO;
		}

		Integer pageSize;
		if (pSize == null) {
			pageSize = Constants.PAGE_SIZE;
		} else {
			switch (pSize) {
				case 0: pageSize = Constants.PAGE_SIZE;
				break;
				case 1: pageSize = Constants.PAGE_SIZE_TWENTY;
				break;
				case 2: pageSize = Constants.PAGE_SIZE_FIFTY;
				break;
				default:pageSize = Constants.PAGE_SIZE;
				break;
			}
		}
		
		// 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
		PageHelper.startPage(pNo, pageSize);
		
		OwnerInfo ownerInfo = new OwnerInfo();
		if (StringUtils.isNotEmpty(name)) {
			ownerInfo.setName(name);
		}
		if (StringUtils.isNotEmpty(lName)) {
            ownerInfo.setAccount(lName);;
        }
		if (StringUtils.isNotEmpty(phoneNo)) {
            ownerInfo.setPhone(phoneNo);;
        }
		ownerInfo.setUserType(Constants.OWNER_TYPE);
		
		List<OwnerInfo> list = ownerInfoService.selectByKeyword(ownerInfo);
		PageInfo<OwnerInfo> info = new PageInfo<OwnerInfo>(list);
		
		model.addAttribute("ownerInfo", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("pSize", pSize);
		model.addAttribute("name", name);
		model.addAttribute("lName", lName);
		model.addAttribute("phoneNo", phoneNo);

		List<Dict> pageSizes = dictService.getDictFromCache("pageSize");
		model.addAttribute("pageSizes", pageSizes);

		return "base/ownerinfo/ownerInfoList";
	}
	
	/**
	 * 
	 * @Description 业主信息详情
	 * @author zhangsh zsh3384027@gmail.com
	 * @date 2016年4月7日
	 * @see
	 */
	@RequestMapping(value= "/ownerInfoDetail")
	public String ownerInfoDetail(Model model, String id){
		try {
			OwnerInfo ownerInfo = ownerInfoService.selectByPrimaryKey(id);
			if(ownerInfo != null){
				model.addAttribute("ownerInfo", ownerInfo);
            }
        } catch (Exception e) {
        }
        return "base/ownerinfo/ownerInfoDetail";
		
	}
	/**
	 * 
	 * @Description 业主账号操作
	 * @author zhangsh zsh3384027@gmail.com
	 * @date 2016年4月7日
	 */
	@RequestMapping(value= "/ownerInfoUpdate", method = RequestMethod.POST)
	@ResponseBody
	public RequestJson ownerInfoUpdate(Model model, String id, int state, HttpServletRequest request){
		 RequestJson result = new RequestJson();
		 User user = (User)request.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
		 
		 int temp = 0;
		 try {
			 OwnerInfo ownerInfo = new OwnerInfo();
			 ownerInfo.setId(id);
			 switch (state) {
			 case STATE_SUCCESS:
				 temp = STAET_FORBIDEN;
				 break;
			 case STAET_FORBIDEN:
				 temp = STATE_SUCCESS;
				 break;
			 default:
				 temp = STAET_FORBIDEN;
				 break;
			 }
			 ownerInfo.setState(temp);
			 ownerInfoService.update(ownerInfo);
			 LogUtils.insertLog("用户状态更新 ", user.getId(), request);
		 } catch (Exception e) {
			 result.setSuccess(false);
		 }
		return result;
	}
	/**
	 * 
	 * @Description 重置密码
	 * @return 
	 * @author zhangsh zsh3384027@gmail.com
	 * @date 2016年4月8日
	 * @see
	 */
	@RequestMapping(value= "/reSetPassword", method = RequestMethod.POST)
	@ResponseBody
	public RequestJson reSetPossword(Model model, String id, String username , String pwd, HttpServletRequest request){
		RequestJson result = new RequestJson();
		 try {
			 OwnerInfo ownerInfo = new OwnerInfo();
			 ownerInfo.setId(id);
//			 String password = MD5Util.generalPassword(username, pwd);
			 ownerInfo.setPassword(pwd);
			 ownerInfoService.update(ownerInfo);
			 LogUtils.insertLog("用户重置密码", ownerInfo.getAccount(), request);
		 } catch (Exception e) {
			 result.setSuccess(false);
		 }
		return result;
	}
	
	/**
	 * @Description 新注册用户
	 * @author zhangsh zsh3384027@gmail.com
	 * @date 2016年4月8日
	 */
	@RequestMapping(value= "/newOwnerInfoList", method=RequestMethod.GET)
	public String getNewOwnerInfo(Model model, Integer pNo, Integer pSize, String name, HttpServletRequest req) {
		if (pNo == null) {
			pNo = Constants.PAGE_NO;
		}

		Integer pageSize;
		if (pSize == null) {
			pageSize = Constants.PAGE_SIZE;
		} else {
			switch (pSize) {
				case 0: pageSize = Constants.PAGE_SIZE;
				break;
				case 1: pageSize = Constants.PAGE_SIZE_TWENTY;
				break;
				case 2: pageSize = Constants.PAGE_SIZE_FIFTY;
				break;
				default:pageSize = Constants.PAGE_SIZE;
				break;
			}
		}
		
		// 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
		PageHelper.startPage(pNo, pageSize);
		
		OwnerInfo ownerInfo = new OwnerInfo();
		if (StringUtils.isNotEmpty(name)) {
			ownerInfo.setName(name);
		}
		ownerInfo.setUserType(Constants.OWNER_TYPE);
		ownerInfo.setState(ManageConstants.STATE_INIT);
		List<OwnerInfo> list = ownerInfoService.selectByKeyword(ownerInfo);
		PageInfo<OwnerInfo> info = new PageInfo<OwnerInfo>(list);
		
		model.addAttribute("ownerInfo", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("pSize", pSize);
		model.addAttribute("name", name);

		List<Dict> pageSizes = dictService.getDictFromCache("pageSize");
		model.addAttribute("pageSizes", pageSizes);

		return "base/ownerinfo/ownerInfoList";
		
	}
	
	/*@RequestMapping(value = "exportTaskInfo.htm")
	public ModelAndView exportOwnerInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.debug("进入导出文件控制器!");
		HttpSession session = request.getSession();
		UserVO userVO = (UserVO) session.getAttribute("userVO");

		Map<String, String> conditionMap = new HashMap<String, String>();

		if (session.getAttribute("taskManagerCondition") != null) {
			conditionMap = (Map<String, String>) session
					.getAttribute("taskManagerCondition");
		}

		Page page = new Page();
		page.setStart(0);
		page.setLimit(99999);

		page = taskManagerServiceItf.findTaskManagerList(page, userVO,
				conditionMap);

		SXSSFWorkbook workBook = taskManagerServiceItf
				.getWorkBookFromTaskList(page.getRoot());

		// 通过Response把数据以Excel格式保存
		response.reset();
		response.setContentType("application/msexcel;charset=UTF-8");
		OutputStream out = null;

		try {
			out = response.getOutputStream();
			response.addHeader("Content-Disposition", "attachment;filename=\""
					+ new String(("新注册业主信息表" + ".xlsx").getBytes("GBK"),
							"ISO8859_1") + "\"");
			workBook.write(out);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.close();
			workBook.dispose();
		}
		return null;
	}*/

}
